import uuid
from dataclasses import dataclass
from typing import Any

import pytest
from sqlalchemy import insert
from sqlalchemy.ext.asyncio import AsyncSession

from recruit.adapter.database.gateway import ResumeEducationGateway
from recruit.adapter.database.models import Resume, ResumeEducation
from .fixtures.resume import ResumeTest
from .utils import check_attr_model, delete_user, check_get_one, get_resume


@dataclass
class ResumeEducationTest:
    id: uuid.uuid4()
    resume_id: uuid.uuid4()
    year: int
    education_institution: str
    faculty: str
    specialization: str
    graduation_year: int


async def _check_resume(
        session: AsyncSession,
        data: ResumeEducationTest
) -> None:
    resume_education = await get_resume(session, ResumeEducation, data.id)
    assert isinstance(resume_education, ResumeEducation)
    check_get_one(
        resume_education,
        data,
        ('id', 'resume_id', 'year', 'education_institution', 'faculty',
         'specialization', 'graduation_year')
    )


async def _create_resume_education(
        session: AsyncSession,
        data: dict[str, Any]
) -> ResumeEducationTest:
    stmt = insert(ResumeEducation).values(**data).returning(ResumeEducation)
    resume_education = (await session.execute(stmt)).scalar_one()
    resume_education = ResumeEducationTest(
        id=resume_education.id,
        resume_id=resume_education.resume_id,
        year=resume_education.year,
        education_institution=resume_education.education_institution,
        faculty=resume_education.faculty,
        specialization=resume_education.specialization,
        graduation_year=resume_education.graduation_year
    )
    await session.commit()
    return resume_education


async def test_create(
        db_session: AsyncSession,
        get_resume_education: dict[str, Any],
        create_resume: ResumeTest
) -> None:
    resume_education_gateway = ResumeEducationGateway(db_session)
    get_resume_education['resume_id'] = create_resume.id
    resume_education = await resume_education_gateway.create(
        **get_resume_education
    )
    assert isinstance(resume_education.resume, Resume)
    check_attr_model(resume_education, get_resume_education)
    await db_session.commit()
    await delete_user(db_session, create_resume.user_id)


@pytest.mark.parametrize(
    'keys', [
        ('id', 'resume_id', 'year', 'education_institution', 'faculty',
         'specialization', 'graduation_year')
    ]
)
async def test_get_one(
        keys: tuple[str, ...],
        db_session: AsyncSession,
        get_resume_education: dict[str, Any],
        create_resume: ResumeTest
) -> None:
    resume_education_gateway = ResumeEducationGateway(db_session)
    get_resume_education['resume_id'] = create_resume.id
    created_resume_education = await _create_resume_education(
        db_session,
        get_resume_education
    )
    for key, value in get_resume_education.items():
        resume_education = await resume_education_gateway.get_one(
            **{key: value}
        )
        assert resume_education is not None
        check_get_one(resume_education, created_resume_education, keys)
    await delete_user(db_session, create_resume.user_id)


async def test_check_resume(
        db_session: AsyncSession,
        get_resume_education: dict[str, Any],
        create_resume: ResumeTest
) -> None:
    resume_education_gateway = ResumeEducationGateway(db_session)
    get_resume_education['resume_id'] = create_resume.id
    created_resume_education = await _create_resume_education(
        db_session,
        get_resume_education
    )
    val = await resume_education_gateway.check_resume(
        created_resume_education.resume_id
    )
    assert val
    await delete_user(db_session, create_resume.user_id)
    val = await resume_education_gateway.check_resume(
        created_resume_education.resume_id
    )
    assert not val


async def test_delete(
        db_session: AsyncSession,
        get_resume_education: dict[str, Any],
        create_resume: ResumeTest
) -> None:
    resume_education_gateway = ResumeEducationGateway(db_session)
    get_resume_education['resume_id'] = create_resume.id
    created_resume_education = await _create_resume_education(
        db_session,
        get_resume_education
    )
    await resume_education_gateway.delete(created_resume_education.id)
    await db_session.commit()
    await delete_user(db_session, create_resume.user_id)
    resume_education = await get_resume(
        db_session,
        ResumeEducation,
        created_resume_education.id
    )
    assert resume_education is None
